﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Data.Entity.Spatial;
using System.Data.Entity.Core.Objects;

namespace TreeManagement
{
    public partial class ViewPanel : UserControl
    {
        public ViewPanel()
        {
            InitializeComponent();
        }
        
        public List<List<Point>> WardPoints;
        public List<List<Point>> RoadPoints;
        public List<Point> Districts;
        public List<Point> Trees;
        public List<Point> RecordedPoints = new List<Point>();
        private List<IRecordPoint>recordPoints = new List<IRecordPoint>();
        public Tuple<Point, int> RRect;
        private void ViewPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics myGraphics = e.Graphics;
            Matrix myMatrix = new Matrix(1, 0, 0, -1, 0, 0);
            //myGraphics.Transform = myMatrix;
            //myGraphics.TranslateTransform(0,300, MatrixOrder.Append);
            // draw coordinate system
           

            if (Districts != null)
            {
                drawDistrict(myGraphics,Districts.ToArray());
            }
            TMUtils.fillMultiplePolygons(myGraphics, WardPoints);
            TMUtils.drawMultipleLines(myGraphics, RoadPoints);
            TMUtils.drawTreePoint(myGraphics, Trees);
            foreach (IRecordPoint r in recordPoints)
                r.drawRecordedPoint(myGraphics);

            if (RRect != null)
                TMUtils.fillEclipse(myGraphics, RRect.Item1, RRect.Item2);

            drawCoordinateSystem(myGraphics);   

            // Create the path.
            //GraphicsPath myGraphicsPath = new GraphicsPath();
            //Rectangle myRectangle = new Rectangle(0, 0, 60, 60);
            //myGraphicsPath.AddRectangle(myRectangle);

            //SolidBrush solidBrush1 = new SolidBrush(Color.Blue);
            // Fill the path on the new coordinate system. 
            // No local transformation
           // myGraphics.FillPath(solidBrush1, myGraphicsPath);
        }
        private void drawCoordinateSystem(Graphics g)
        {
            Brush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(brush,1);
            g.DrawLine(pen, new Point(0, 0), new Point(0, this.Height));
            g.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));

            System.Drawing.Font font = new System.Drawing.Font("Arial", 10);
            //draw X:
            int x = 0;
            while (x < this.Width)
            {
                g.DrawRectangle(pen, new Rectangle(x, 0, 2, 2));
                g.DrawString(x+"", font, brush, new Point(x, 0));
	    	    x+=TMConsts.COORDINATE_DELIMER;
            }
            int y = 0;
            while (y < this.Height)
            {
                g.DrawRectangle(pen, new Rectangle(0, y, 2, 2));
                g.DrawString(y + "", font, brush, new Point(0, y));
                y += TMConsts.COORDINATE_DELIMER;
            }
            
        }
        private void drawString(Graphics g, string stringText, Brush brush)
        {
            FontFamily family = new FontFamily("Arial");
            int fontStyle = (int)FontStyle.Italic;
            int emSize = 26;
            Point origin = new Point(100, 100);
            StringFormat format = StringFormat.GenericDefault;

            // Add the string to the path.
            GraphicsPath myPath = new GraphicsPath();
            myPath.AddString(stringText,
                family,
                fontStyle,
                emSize,
                origin,
                format);
            Matrix mt = new Matrix();
            mt.Rotate(0);
            myPath.Transform(mt);
            g.FillPath(brush, myPath);
        }
        private void drawDistrict(Graphics g, Point[]points)
        {                 
            SolidBrush solidBrush1 = new SolidBrush(Color.Gray);           
            if (points.Length > 0)
                g.FillPolygon(solidBrush1, points);
        }
        
     
       
        
        private void ViewPanel_Load(object sender, EventArgs e)
        {
            
        }

        public void registerRecodePoint(IRecordPoint recordPoint){
            recordPoints.Add(recordPoint);
        }
        public void unregisterRecordedPoint(IRecordPoint recordedPoint)
        {
            recordPoints.Remove(recordedPoint);
        }
        private void notifyRecordPoint(Point p)
        {
            foreach (IRecordPoint r in recordPoints)
                r.notifyRecordPoint(p);
        }
       
        private void ViewPanel_MouseDown(object sender, MouseEventArgs e)
        {
            notifyRecordPoint(e.Location);
        }

    }
}
